package com.parking.admin.service;

import com.parking.admin.entity.History;
import com.parking.admin.entity.Parking;
import com.parking.admin.repository.HistoryRepository;
import com.parking.admin.repository.ParkingRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * Created by hujianjun on 2015/11/9.
 */
@Service
public class ParkingService {

    @Autowired
    private ParkingRepository parkingRepository;

    @Autowired
    private HistoryRepository historyRepository;

    public Page<Parking> findAll(Pageable pageable, String key) {
        if (key==null || key.isEmpty())
            return parkingRepository.findAll(pageable);
        return parkingRepository.findByKey(pageable, key);
    }
    public List<Parking> findAll() {
        return parkingRepository.findAll();
    }
    public Parking findOne(long idParking) {
        return parkingRepository.findOne(idParking);
    }

    public int saveLngLat1(String editor, long idParking, String lng, String lat) {
        parkingRepository.updateLngLat1(editor, new Date(), idParking, lng, lat);
        History history = new History();
        history.setEditor(editor);
        history.setIdParking(idParking);
        history.setLng(lng);
        history.setLat(lat);
        history.setLastTime(new Date());
        historyRepository.save(history);
        return 0;
    }

    @Transactional
    public int saveLngLat(long idParking, String lng, String lat) {
        parkingRepository.updateLngLat(idParking, lng, lat);

        return 0;
    }

    @Transactional
    public int saveAll(long idParking, String changeLevel,  String changeAddress, String changeLng, String changeLat) {
        if(parkingRepository != null && changeLevel != null && changeAddress != null && changeLng != null && changeLat != null) {
            parkingRepository.updateParkingAll( idParking, changeLevel,  changeAddress, changeLng, changeLat);
        } else {
            int i = 0;
            i++;
        }
        return 0;
    }

}
